{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}
{% from 'admin/lib.html' import extra with context %} {# backward compatible #}
{% block body %}
    <div class="row">
    <div class="col-md-6">
        <form id="test_form" action="{{ url_for('domain.save_debug_crawl_rule_result',id=id) }}" method="POST" role="form" class="admin-form form-horizontal" enctype="multipart/form-data">
        {{ lib.render_form_fields(form, form_opts=None)  }}
        </form>
    </div>
  <div class="col-md-6">
      <button type="button" id="run_test_crawl_rule" class="btn btn-primary">运行测试</button>
      <button type="button" id="save_crawl_rule" class="btn btn-primary">保存结果</button>
      <span id="run_status"></span>
      <div id="test_result">
        测试结果
          {{ url_for('domain.return_debug_crawl_rule_result') }}
      </div>
  </div>
    </div>
{% endblock %}
{% block tail %}
    <script>
    var HtmlUtil = {
        /*1.用浏览器内部转换器实现html转码*/
        htmlEncode:function (html){
            //1.首先动态创建一个容器标签元素，如DIV
            var temp = document.createElement ("div");
            //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐，google支持)
            (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);
            //3.最后返回这个元素的innerHTML，即得到经过HTML编码转换的字符串了
            var output = temp.innerHTML;
            temp = null;
            return output;
        },
        /*2.用浏览器内部转换器实现html解码*/
        htmlDecode:function (text){
            //1.首先动态创建一个容器标签元素，如DIV
            var temp = document.createElement("div");
            //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
            temp.innerHTML = text;
            //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐，google支持)，即得到经过HTML解码的字符串了。
            var output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        },
        /*3.用正则表达式实现html转码*/
        htmlEncodeByRegExp:function (str){
             var s = "";
             if(str.length == 0) return "";
             s = str.replace(/&/g,"&");
             s = s.replace(/</g,"<");
             s = s.replace(/>/g,">");
             s = s.replace(/ /g," ");
             s = s.replace(/\'/g,"'");
             s = s.replace(/\"/g,'"');
             return s;
       },
       /*4.用正则表达式实现html解码*/
       htmlDecodeByRegExp:function (str){
             var s = "";
             if(str.length == 0) return "";
             s = str.replace(/&/g,"&");
             s = s.replace(/</g,"<");
             s = s.replace(/>/g,">");
             s = s.replace(/ /g," ");
             s = s.replace(/'/g,"\'");
             s = s.replace(/"/g,"\"");
             return s;
       }
    };
    $('#run_test_crawl_rule').click(function(){
        bookurl=$('#test_form #BookUrl').val();
        $('#run_status').text('');
        $('#test_result').text('');
        if(!/^https?:\/\/.*$/.test(bookurl))
        {
            alert('测试书籍页路径错误!');
        }
        else
        {
            $('#run_status').text('正在处理');
            var formData = new FormData($('#test_form')[0]);
            $.ajax({
                type: "POST",
                url:"{{ url_for('domain.return_debug_crawl_rule_result') }}",
                data: formData,
                contentType: false,
                processData: false,
                dataType:"json",
                success: function(data) {
                    $('#run_status').text('处理完毕');
                    if(data.status=="error")
                        $('#test_result').text(data.data)
                    else
                        $.each(data.data,function(k,v){
                            $('#test_result').append("<br>");
                            $('#test_result').append('<font style="color:red;">'+k+"</font>");
                            $('#test_result').append(" = ");
                            $('#test_result').append(HtmlUtil.htmlEncode(v));

                        })
                        //$('#test_result').text(JSON.stringify(data.data))
                }
            });
        }
    });
    $('#save_crawl_rule').click(function(){
        var formData = new FormData($('#test_form')[0]);
        $.ajax({
                type: "POST",
                url:$('#test_form').attr('action'),
                data: formData,
                contentType: false,
                processData: false,
                dataType:"json",
                success: function(data) {
                    alert(data);
                }
            });
    });
    </script>
{% endblock %}